import React, { Component } from 'react';
import Child from './Child'

export default class Parent extends Component {
  state = {
    hasError: ""//用于标识子组件是否产生错误
  }
  static getDerivedStateFromError(error){
    console.log("@@",error)
    return {hasError: error}
  }
  componentDidCatch(){
    console.log("渲染组件时出错")
  }
  render() {
    const {hasError} = this.state
    return <div>
      <h3>我是Parent组件</h3>
      {hasError?<h3>当前网络不稳定，请稍后再试</h3>: <Child/>}
    </div>;
  }
}